package com.platform.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platform.dao.MiHealthDataDao;
import com.platform.dao.UserDao;
import com.platform.entity.HealthDataEntity;
import com.platform.entity.MiHealthDataEntity;
import com.platform.entity.UserEntity;
import com.platform.utils.RRException;

/**
 * 小米体脂称健康数据Service实现类
 *
 * @author LarkerJin
 * @email 277901879@qq.com
 * @date 2019-10-21 14:15:38
 */
@Service("miHealthDataService")
public class MiHealthDataService {
	public static final Pattern pattern = Pattern.compile("[^0-9.\\.]");

	@Autowired
    private MiHealthDataDao miHealthDataDao;
    @Autowired
    private UserDao userDao;

    public MiHealthDataEntity saveFromJson(JSONObject res,Integer userId){
//    	-1 未达标，1 达标，0 需关注
    	Integer explain = -1;
		if(res.has("words_result")) {
	    	MiHealthDataEntity miHealthData = new MiHealthDataEntity();
    		JSONArray jsonArray = res.getJSONArray("words_result");
    		for(int i=1; i<jsonArray.length(); i++) {
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("身体得分")>=0) {
    				miHealthData.setNickName(jsonArray.getJSONObject(i-1).getString("words"));
    				if(userId == null) {
    					Map<String,Object> params = new HashMap<String,Object>();
    					params.put("usernameB", miHealthData.getNickName());
    					List<UserEntity> users = userDao.queryList(params);
    					if(users == null || users.size() == 0) {
    						throw new RRException("无该会员信息，请核实是否修改昵称！" + miHealthData.getNickName());
    					}
    					if(users.size() > 1) {
    						throw new RRException("多个会员昵称重名，无法匹配！" + miHealthData.getNickName());
    					}
    					userId = users.get(0).getId();
    				}
    				miHealthData.setUserId(userId);
    				miHealthData.setCreateDate("" + jsonArray.getJSONObject(i + 3).getString("words"));
    				
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i + 4).getString("words"));
			        String data = matcher.replaceAll("");
			        miHealthData.setWeight(BigDecimal.valueOf(Double.valueOf(data)));
			        miHealthData.setWeightDesc(jsonArray.getJSONObject(i + 2).getString("words"));
			        miHealthData.setWeightTarget(jsonArray.getJSONObject(i + 5).getString("words"));
			        matcher = pattern.matcher(jsonArray.getJSONObject(i + 1).getString("words"));
			        miHealthData.setScore(Integer.valueOf(matcher.replaceAll("")));
    				i = i+6;
    				continue;
    			}
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("未达标指标")>=0) {
    				explain = -1;
    				continue;
    			}
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("需关注指标")>=0) {
    				explain = 0;
    				continue;
    			}
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("健康指标")>=0) {
    				explain = 1;
    				continue;
    			}
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("BM")>=0) {
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
    				miHealthData.setBmi(BigDecimal.valueOf(Double.valueOf(data)));
    				miHealthData.setBmiDesc(jsonArray.getJSONObject(i + 1).getString("words"));
    				miHealthData.setBmiExplain(explain);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("体脂")>=0) {
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
			        miHealthData.setBodyfat(BigDecimal.valueOf(Double.valueOf(data)));
			        miHealthData.setBodyfatDesc(jsonArray.getJSONObject(i + 1).getString("words"));
			        miHealthData.setBodyfatExplain(explain);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("水分")>=0) {
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
			        miHealthData.setBodyWater(BigDecimal.valueOf(Double.valueOf(data)));
			        miHealthData.setBodyWaterDesc(jsonArray.getJSONObject(i + 1).getString("words"));
			        miHealthData.setBodyWaterExplain(explain);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("蛋白质")>=0) {
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
			        miHealthData.setProtein(BigDecimal.valueOf(Double.valueOf(data)));
			        miHealthData.setProteinDesc(jsonArray.getJSONObject(i + 1).getString("words"));
			        miHealthData.setProteinExplain(explain);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("基础代谢")>=0) {
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("").replaceAll(",", "");
			        miHealthData.setBasalMetabolism(Integer.valueOf(data));
			        miHealthData.setBasalMetabolismDesc(jsonArray.getJSONObject(i + 1).getString("words"));
			        miHealthData.setBasalMetabolismExplain(explain);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("肌肉")>=0) {
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
			        miHealthData.setMuscle(BigDecimal.valueOf(Double.valueOf(data)));
			        miHealthData.setMuscleDesc(jsonArray.getJSONObject(i + 1).getString("words"));
			        miHealthData.setMuscleExplain(explain);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("内脏脂肪")>=0) {
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
			        miHealthData.setVisceralFat(Integer.valueOf(data));
			        miHealthData.setVisceralFatDesc(jsonArray.getJSONObject(i + 1).getString("words"));
			        miHealthData.setVisceralFatExplain(explain);
    				i = i+1;
    				continue;
    			}
    			
    			if(jsonArray.getJSONObject(i).getString("words").indexOf("骨量")>=0) {
			        Matcher matcher = pattern.matcher(jsonArray.getJSONObject(i).getString("words"));
			        String data = matcher.replaceAll("");
			        miHealthData.setBoneMass(BigDecimal.valueOf(Double.valueOf(data)));
			        miHealthData.setBoneMassDesc(jsonArray.getJSONObject(i + 1).getString("words"));
			        miHealthData.setBoneMassExplain(explain);
    				i = i+1;
    				continue;
    			}
    		}
    		
        	Map<String,Object> params = new HashMap<String,Object>();
        	params.put("userId", userId);
        	params.put("isInit", 1);
    		List<MiHealthDataEntity> initMiHealthDatas = miHealthDataDao.queryList(params);
    		if(initMiHealthDatas == null || initMiHealthDatas.size()==0) {
    			miHealthData.setIsInit(1);
    		}else {
    			miHealthData.setIsInit(0);
    		}
    		
    		miHealthDataDao.save(miHealthData);
    		return miHealthData;
		}
		return null;
	
    }
    
    public MiHealthDataEntity queryObject(Integer id) {
        return miHealthDataDao.queryObject(id);
    }

    public List<MiHealthDataEntity> queryList(Map<String, Object> map) {
        return miHealthDataDao.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return miHealthDataDao.queryTotal(map);
    }

    public int save(MiHealthDataEntity miHealthData) {
        return miHealthDataDao.save(miHealthData);
    }

    public int update(MiHealthDataEntity miHealthData) {
        return miHealthDataDao.update(miHealthData);
    }

    public int delete(Integer id) {
        return miHealthDataDao.delete(id);
    }

    public int deleteBatch(Integer[] ids) {
        return miHealthDataDao.deleteBatch(ids);
    }
}
